/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.VersionSchema;
import han.bifang.gwt.client.common.util.StringUtils;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceMessages;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;

/**
 * 
 * @Author Kent
 */
public class VersionSchemaContent extends Content {

	
	private ServiceMessages cons;
	
	private BifangGWTServiceAsync service;

	private VerticalPanel panel = null;
	
	private TextArea request, response;
	
	private TextArea desc;
	
	private String versionId;
	
	private boolean loaded;
	
	private AsyncCallback<ResponseObject> cb1 = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code1 = res.getCode();
			if (!code1.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			Window.alert(cons.modifyOK());

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			VersionSchema vs = (VersionSchema)res.getResponse();
			
			request.setText(vs.getRequestSchema());			
			response.setText(vs.getResponseSchema());
			desc.setText(vs.getDescription());

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	
	public VersionSchemaContent(ServiceMessages cons,BifangGWTServiceAsync service,
			String versionId) {
		this.cons = cons;
		this.service = service;
		
		panel = new VerticalPanel();
		panel.setSpacing(5);
		panel.setWidth("100%");
		this.versionId = versionId;

//		HTML h = new HTML(cons.wsdl());
//		panel.add(h);
		
//		TextArea t = new TextArea();
//		t.setWidth("100%");
//		t.setText(wsdl);
//		t.setVisibleLines(20);
		
		
		
		initWidget(panel);

	}
	
	public void init(){
		if(loaded)
			return ;
		Grid table = new Grid(3,2);
		table.setCellPadding(1);
		table.setCellSpacing(1);
		table.setBorderWidth(1);
		ColumnFormatter cf = table.getColumnFormatter();
		cf.setWidth(0, "15%");
		cf.setWidth(1, "55%");

		CellFormatter ff = table.getCellFormatter();
		ff.setStyleName(0, 0, "tablecol");
		ff.setStyleName(1, 0, "tablecol");
		ff.setStyleName(2, 0, "tablecol");
		
		table.setHTML(0, 0, cons.requestSchema());
		table.setHTML(1, 0, cons.responseSchema());
		table.setHTML(2, 0, cons.desc());
		
		request = new TextArea();
		request.setCharacterWidth(64);
		request.setVisibleLines(6);
		//
		table.setWidget(0, 1, request);
		
		response = new TextArea();
		response.setCharacterWidth(64);
		response.setVisibleLines(6);
		//
		table.setWidget(1, 1, response);
		
		desc = new TextArea();
		desc.setCharacterWidth(64);
		desc.setVisibleLines(6);
		//
		
		table.setWidget(2, 1, desc);
		
		panel.add(table);
		
		Button b = new Button(cons.modifySchema());
		b.addClickListener(new ClickListener() {
			public void onClick(Widget send) {
				if(!validate()){
					return;
				}
				VersionSchema vs = new VersionSchema();
				vs.setVersionId(versionId);
				vs.setRequestSchema(request.getText());
				vs.setResponseSchema(response.getText());
				vs.setDescription(desc.getText());
				
				
				RequestObject req = new RequestObject();
				req
						.setAction("han.bifang.gwt.server.action.service.ModifyVersionSchemaAction");
				req.setRequest(vs);
				service.call(req, cb1);

			}
		});
		panel.add(new HTML("&nbsp;"));
		panel.add(b);
		RequestObject req = new RequestObject();
		req
				.setAction("han.bifang.gwt.server.action.service.GetVersionSchemaAction");
		req.setRequest(versionId);
		service.call(req, cb);
		
		loaded = true;

	}

	private boolean validate() {
		if (!StringUtils.checkLenght(desc.getText(), 1, 1024)) {
			Window.alert(cons.lengthError(cons.desc(), "1", "1024"));
			return false;
		}
		
		
		
		return true;

	}
	

}
